const express = require('express');
const router = express.Router();
const { get,post } = require('../util/http');

// 设置是否禁ping
router.all('/GetDir', (req, res) => {
    let params = req.body;
    post('GetDir', {
        path: typeof params.path != 'undefined' ? params.path : '/',
    }, {}, {
        p: typeof params.page != 'undefined' ? params.page : '1',
        showRow: typeof params.pageSize != 'undefined' ? params.pageSize : '10',
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

//计算文件大小
router.all('/GetPathSize', (req, res) => {
    let params = req.body;
    post('GetPathSize', {
        path: typeof params.path != 'undefined' ? params.path : '/',
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

//创建文件
router.all('/CreateFile', (req, res) => {
    let params = req.body;
    post('CreateFile', {
        path: typeof params.path != 'undefined' ? params.path : '/',
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

//创建目录
router.all('/CreateDir', (req, res) => {
    let params = req.body;
    post('CreateDir', {
        path: typeof params.path != 'undefined' ? params.path : '/',
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

//删除文件
router.all('/DeleteFile', (req, res) => {
    let params = req.body;
    post('DeleteFile', {
        path: typeof params.path != 'undefined' ? params.path : '/',
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

//删除文件
router.all('/DeleteDir', (req, res) => {
    let params = req.body;
    post('DeleteDir', {
        path: typeof params.path != 'undefined' ? params.path : '/',
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

//文件重命名
router.all('/RenameFile', (req, res) => {
    let params = req.body;
    post('MvFile', {
        sfile: typeof params.sfile != 'undefined' ? params.sfile : '',
        dfile: typeof params.dfile != 'undefined' ? params.dfile : '',
        rename: true,
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

//下载文件
router.get('/Download', (req, res) => {
    get('Download',{
        filename:typeof req.query.filename!='undefined'?req.query.filename:'',
    }).then(response => {
        res.send(response);
    }).catch((e) => {
        res.send(e)
    });
})

module.exports = router;